package com.qwert.shortlink.admin.remote.dto;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qwert.shortlink.admin.common.convention.result.Result;
import com.qwert.shortlink.admin.remote.dto.req.shortlink.ShortLinkCreateReqDTO;
import com.qwert.shortlink.admin.remote.dto.req.shortlink.ShortLinkPageReqDTO;
import com.qwert.shortlink.admin.remote.dto.req.shortlink.ShortLinkUpdateReqDTO;
import com.qwert.shortlink.admin.remote.dto.resp.shortlink.ShortLinkCountRespDTO;
import com.qwert.shortlink.admin.remote.dto.resp.shortlink.ShortLinkCreateRespDTO;
import com.qwert.shortlink.admin.remote.dto.resp.shortlink.ShortLinkPageRespDTO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短链接中台远程调用服务
 * @author qwert
 * @version 1.0.0
 * @create 2024/7/3 9:47
 **/
public interface ShortLinkRemoteService {

    /**
     *  远端调用分页查询短链接
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO shortLinkPageReqDTO){
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid", shortLinkPageReqDTO.getGid());
        requestMap.put("current", shortLinkPageReqDTO.getCurrent());
        requestMap.put("size", shortLinkPageReqDTO.getSize());
        String resultPage = HttpUtil.get("http://localhost:8001/api/shortlink/v1/page", requestMap);
        return JSON.parseObject(resultPage, new TypeReference<>() {
        });
    }
    /**
     * 创建短链接
     */
    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO shortLinkCreateReqDTO){
        String resultCreate = HttpUtil.post("http://localhost:8001/api/shortlink/v1/create", JSON.toJSONString(shortLinkCreateReqDTO));
        return JSON.parseObject(resultCreate, new TypeReference<>() {
        });

    }

    default Result<List<ShortLinkCountRespDTO>> listGroupShortLinkCount(List<String> gids){
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("gids", gids);
        String resultCountList = HttpUtil.get("http://localhost:8001/api/shortlink/v1/count", requestMap);
        return JSON.parseObject(resultCountList, new TypeReference<>() {
        });

    }

    default Result<Integer> updateShortLink(ShortLinkUpdateReqDTO shortLinkUpdateReqDTO){
        String resultUpdate = HttpUtil.post("http://localhost:8001/api/shortlink/v1/update",JSON.toJSONString(shortLinkUpdateReqDTO));
        return JSON.parseObject(resultUpdate, new TypeReference<>() {
        });
    }
}
